package com.chunxuan.langquan.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.dao.bean.SubjectBean1;
import com.chunxuan.langquan.dao.bean.SubjectBean2;
import com.chunxuan.langquan.support.util.CenterText;
import com.chunxuan.langquan.support.util.Logg;
import com.chunxuan.langquan.ui.activity.CourseDetailActivity;
import com.chunxuan.langquan.ui.activity.SubjectActivity;
import com.chunxuan.langquan.ui.activity.SubjectListMoreActivity;

import java.util.List;

/**
 * 科目页adapter
 */
public class SubjectAdapter extends RecyclerView.Adapter {
    private SparseArray<SubjectBean2> titles = new SparseArray<>();
    private Context mContext;
    private List<SubjectBean1> subjectBean1List;

    public SubjectAdapter(Context context, List<SubjectBean1> subjectBean1List) {
        this.mContext = context;
        this.subjectBean1List = subjectBean1List;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (isTitle(viewType)) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_rlv_course_title, parent, false);
            TitleViewHolder titleViewHolder = new TitleViewHolder(v);
            return titleViewHolder;
        } else {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_rlv_course, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(v);
            return myViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final SubjectActivity subjectActivity = (SubjectActivity) mContext;
        final int p = position;
        if (isTitle(position)) {
            TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
            titleViewHolder.tvSubjectTitle.setText(titles.get(position).getName());
            titleViewHolder.tvSubjectMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        Intent intent = new Intent(subjectActivity, SubjectListMoreActivity.class);
//                        intent.putExtra("grade_category_id", subjectBean1List.get(p).getGrade_category_id() + "");
                        intent.putExtra("grade_category_id", 0 + "");
//                        intent.putExtra("subject_category_id", subjectBean1List.get(p).getSubject_category_id() + "");
                        intent.putExtra("subject_category_id", titles.get(p).getClass_category_id() + "");
//                        intent.putExtra("class_category_id", subjectBean1List.get(p).getClass_category_id() + "");
                        intent.putExtra("class_category_id", 0 + "");
//                        intent.putExtra("course_id", subjectBean1List.get(p).getId() + "");
                        intent.putExtra("course_id", 0 + "");
                        intent.putExtra("title", titles.get(p).getName() + "");
                        subjectActivity.startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            return;
        }
        for (int i = 0; i < titles.size(); i++) {
            int key = titles.keyAt(i);
            if (position > titles.keyAt(titles.size() - 1)) {  //item在最后一个title之后的情况
                position -= titles.size();
                break;
            } else if (key < position && position < titles.keyAt(i + 1)) { //item在某两个title之间的情况
                position -= (i + 1);
                break;
            }
        }
        final int s = position;
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        if(subjectBean1List.get(s).getTitle().toString().length()>13){
            myViewHolder.tvCourseTitle.setText("     "+subjectBean1List.get(s).getTitle());
        }else{
            myViewHolder.tvCourseTitle.setText(subjectBean1List.get(s).getTitle());
        }

//        Logg.i("position , s = " + position + ",,," + s);
//        Logg.i("subjectBean1List.toString() = " + subjectBean1List.toString());

        if(subjectBean1List.get(s).getSummary().toString().length()>15){
            myViewHolder.tvSubjectSummary.setText("     "+subjectBean1List.get(s).getSummary());
        }else{
            myViewHolder.tvSubjectSummary.setText(subjectBean1List.get(s).getSummary());
        }
        GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) myViewHolder.myItemView.getLayoutParams();
        if (s % 2 == 0) {
            lp.leftMargin = 26;
            lp.rightMargin = 13;
        } else if (s % 2 == 1) {
            lp.leftMargin = 13;
            lp.rightMargin = 26;
        }
        lp.topMargin = 20;
        myViewHolder.myItemView.setLayoutParams(lp);
        myViewHolder.myItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(subjectActivity, CourseDetailActivity.class);
                intent.putExtra("grade_category_id", subjectBean1List.get(s).getGrade_category_id() + "");
                intent.putExtra("subject_category_id", subjectBean1List.get(s).getSubject_category_id() + "");
                intent.putExtra("class_category_id", subjectBean1List.get(s).getClass_category_id() + "");
                intent.putExtra("course_id", subjectBean1List.get(s).getId() + "");
                subjectActivity.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (subjectBean1List == null || titles == null) {
            return 0;
        } else {
            return subjectBean1List.size() + titles.size();
        }
    }

    public void addTitle(int position, SubjectBean2 title) {
        titles.put(position, title);
    }

    @Override
    public int getItemViewType(int position) {
        if (isTitle(position)) {
            return position;  //返回title的位置，仅用作onCreateViewHolder方法中的判断--是title
        } else {
            return position + 100000; //返回title+100000的位置，仅用作onCreateViewHolder方法中的判断--不是title
        }
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        //如果是title就占据设置的spanCount个单元格
        final GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
        //Sets the source to get the number of spans occupied by each item in the adapter.
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (isTitle(position)) {
                    return layoutManager.getSpanCount();
                }
                return 1;
            }
        });
    }

    private boolean isTitle(int position) {
        return titles.get(position) == null ? false : true;
    }

    public static class TitleViewHolder extends RecyclerView.ViewHolder {
        TextView tvSubjectTitle;
        TextView tvSubjectMore;

        public TitleViewHolder(View itemView) {
            super(itemView);
            tvSubjectTitle = itemView.findViewById(R.id.tv_subject_title);
            tvSubjectMore = itemView.findViewById(R.id.tv_subject_more);
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvCourseTitle;
        TextView tvSubjectSummary;
        View myItemView;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvCourseTitle = itemView.findViewById(R.id.tv_course_title);
            tvSubjectSummary = itemView.findViewById(R.id.tv_subject_summary);
            myItemView = itemView;
        }
    }
}
